package com.af.monitorModule.generator.service.impl;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Company;
import com.af.monitorModule.generator.entity.Server;
import com.af.monitorModule.generator.mapper.CompanyMapper;
import com.af.monitorModule.generator.mapper.ServerMapper;
import com.af.monitorModule.generator.service.ServerService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyz_4dou
 * @since 2021-02-27
 */
@Service
public class ServerServiceImpl extends ServiceImpl<ServerMapper, Server> implements ServerService {

    @Autowired
    private ServerMapper serverMapper;

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public JSONObject deleteOne(JSONObject jsonObject) {
        if (jsonObject == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        Integer id = jsonObject.getInteger("id");
        Server server = serverMapper.selectById(id);
        server.setState(0);
        serverMapper.updateById(server);
        return ResultEnum.OK.getResult();
    }

    @Override
    public JSONObject updateServer(Server server, String id) {
        if (server == null || id == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        server.setId(Integer.valueOf(id));
        serverMapper.updateById(server);
        return ResultEnum.OK.getResult();
    }

    @Override
    public JSONObject batchDelete(JSONObject ids) {
        if (ids == null){
            return ResultEnum.PARAM_INCORRECRT.getResult();
        }
        JSONArray ids1 = ids.getJSONArray("ids");
        StringBuffer stringBuffer = new StringBuffer();
        ids1.forEach((v)->{
            stringBuffer.append(","+v);
        });
        serverMapper.stopBatch("(" + stringBuffer.toString().substring(1) + ")");
        return ResultEnum.OK.getResult();
    }

    @Override
    public Company findCompanyByServerId(String serverId) {
        Server server = serverMapper.selectById(serverId);
        return companyMapper.selectById(server.getCompanyId());
    }
}
